﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestGit4__3and4_WhileorForSumofFraction
{
    class Program
    {
        /// <summary>
        /// 分数求和
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //1+1/2+2/3+...+99/100=?
            double sum1 = 1;
            for (double i = 1; i < 100; i++)
            {
                sum1 = sum1 + (i / (i + 1));
            }
            Console.WriteLine($"1+1/2+2/3+...+99/100={sum1}");

            //4*(1-1/3+1/5-1/7+...+1/n)=?(do-while)
            double sum2 = 0;
            double n = 1;
            double j;
            do
            {
                //表示每一项
                j = 1 / (2 * n - 1);
                if (n % 2 == 0)
                {
                    j = j * (-1);//区分正负项
                }
                sum2 += j;
                n++;
            }
            while ((1 / (2 * n - 1)) > 0.000001);//满足1/n的条件（不可以用j,j正负未定）
            double PI = 4 * sum2;
            Console.WriteLine($"4*(1-1/3+1/5-1/7+...+1/n)={PI}");




            Console.ReadKey();
        }
    }
}
